home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / INFO / PCCDEMO.ZIP / COMP1.EXE / BATCH.PRS < prev    next >
Text File  |  1993-12-20  |  12KB  |  199 lines

  1.                                                üÇôéç àêïäÆ ╬╠╬╠╧╧╬╠╬╠╬╠╬╠╬╠╡
  2.                                    
  3.                                    │     In this edition,  we will give
  4.                                    │  you a  comprehensive introduction
  5. ┌─┐         ┌─┐        ┌─┐         │  to  BATCH   FILES,  and   how  to
  6. │ └──┐┌────┐│ └┐ ┌────┐│ └──┐      │  harness  their  power.  In future
  7. │ ┌┐ │├─── ││ ┌┘ │ ┌──┘│ ┌┐ │      │  issues, we  will show  you how to
  8. │ └┘ ││ ── ││ └─┐│ └──┐│ ││ │      │  give your batch  files more power
  9. └────┘└────┘└───┘└────┘└─┘└─┘      │  and speed.  As  well  as bringing
  10.     ┌───┐   ┌─┐                    │  you batch file  tricks and hints,
  11.     │ ┌─┘┌─┐│ │┌────┐┌────┐        │  that will change  the way you use
  12.     │ └┐ │ ││ ││ ── ││  ──┤        │  batch files.
  13.     │ ┌┘ │ ││ ││ ───┤├──  │        │
  14.     │ │  └─┘└─┘└────┘└────┘        │
  15.     └─┘    σε± ∞≥-πε≥              │
  16.                                    │     One  of   the   most  powerful
  17.          ß√ απα∞ ΘΣφΩΦφ≥           │  features of Microsoft's MS-DOS is
  18.                                    │  undoubtedly   its    ability   to
  19.                                    │  perform batch  processing.   This
  20. ∞α≥≤Σ±Φφµ ßα≤Γτ σΦδΣ≥ ∞απΣ Σα≥√    │  may    seem     strange,    since
  21. α ∞⌠≥≤ σε± αφ√ ∞≥-πε≥ ⌠≥Σ±.        │  historically batch processing was
  22.                                    │  an unwieldy  process which became
  23.             
  24.  
  25. largely  replaced  by interactive  │  For   small   tasks,   writing  a
  26. processing once  faster computers  │  program  in  a  powerful language
  27. were developed.    The  days when  │  like  Borland's  Turbo  Pascal or
  28. using     a     computer    meant  │  Microsoft  C  is  overkill.   The
  29. laboriously  writing  programs on  │  programs    can    become    more
  30. paper, using a card punch machine  │  complicated   than    is   really
  31. to prepare punched cards, booking  │  necessary and writing Pascal or C
  32. a time to feed them into the card  │  programs   requires   a  suitable
  33. reader of a computer, and collect  │  compiler.  A  good solution is to
  34. the results  some time  later are  │  write  a  "program"  using MS-DOS
  35. long gone.    Now  it's  mostly a  │  commands in a batch file.
  36. case  of  typing  a  command  and  │
  37. getting instant results.   So why  │     Most  operating  systems  have
  38. use batch processing at all?       │  facilities for  batch processing;
  39.                                    │  DEC's VMS has the Digital Command
  40.    Well   there   are   obviously  │  Language and  Unix has  the Korn,
  41. limits   to   what   single   DOS  │  Bourne and C  shells.  The beauty
  42. commands can  do,  even including  │  of batch  programs  is  that they
  43. the use of  pipes and filters (eg  │  are easy  to  write and  use, and
  44. "dir |  more"  or "dir  | sort").  │  quite adequate  for  small tasks,
  45.  
  46.  
  47. making use of  your PC easier and  │     Almost  any   DOS  application
  48. more rewarding.   Why spend hours  │  that you use can be called from a
  49. learning a  new computer language  │  batch file to make it work better
  50. when  you  are  already competent  │  for you. Any command you can give
  51. and comfortable with MS-DOS?       │  to DOS  using  a keyboard  can be
  52.                                    │  placed in  a batch  file and runs
  53.    The latest  version  of MS-DOS  │  just as if you keyed it in.
  54. is version  6,  although  most of  │
  55. the  new  features  of  version 6  │     Let's consider  a simple batch
  56. don't    affect     batch    file  │  file.   It  is  called CHECK.BAT.
  57. processing much, a CHOICE command  │  Note that all  MS-DOS batch files
  58. has been added.  This means, that  │  have   the   extension   .BAT  to
  59. now   you    can    create   real  │  distinguish   them   from   other
  60. interactive  batch   files,  with  │  files.  The contents of CHECK.BAT
  61. menus, questions, etc..            │  (viewable  by  "type  check.bat")
  62.    You  could  use  programs like  │  are:
  63. the antivirus checker  in a batch  │
  64. file; for  example, to  check for  │  @echo off
  65. viruses every  time  you  turn on  │  cls
  66. your computer.                     │  echo This program checks multiple
  67.  
  68.  
  69. disks in the A drive               │  then run.   Files  with   .COM or
  70. :here                              │  .EXE are  always run  before .BAT
  71. echo Insert next disk and          │  files,  thus   if   CHECK.COM  or
  72. pause                              │  CHECK.EXE  exists  you  must type
  73. chkdsk a: /f                       │  "check.bat"  to   run   the batch
  74. goto here                          │  file.
  75.                                    │
  76.    This file could  be created by  │     The  batch   file   will  keep
  77. typing "copy  con  check.bat" and  │  checking   disks   in   drive  A,
  78. then the contents,  followed by a  │  pausing each time  and asking the
  79. Control-Z on a  line by itself to  │  user to  press a  key when ready.
  80. end the file.                      │  To  exit   the   batch  file  you
  81.                                    │  can    use  the  Control-Break or
  82.    To  run   the  batch file just  │  Control-C key  sequence. You will
  83. type "check" by itself at the DOS  │  then    be    prompted "Terminate
  84. prompt, while    in    the   same  │  batch job    Y/N?"    where   you
  85. directory    as    the  check.bat  │  must respond   Y  if  you want to
  86. file.   Unless   a   file  exists  │  quit, or  N  to  continue running
  87. called       "check.com"       or  │  the batch program.
  88. "check.exe",   the   file    will  │
  89.  
  90.  
  91.    Starting a  command   with  an  │  end containing  the command "echo
  92. @   character   stops   DOS  from  │  on".
  93. echoing (showing)   that  command  │
  94. when the batch  program runs. For  │     The PAUSE command when used by
  95. instance, in  the  above example,  │  itself as in this program, simply
  96. the user does  not see "echo off"  │  displays "Strike  a  key"  but if
  97. on the  screen   as that  command  │  followed by text then it displays
  98. runs.   And    they   do  not see  │  the   text   first.      See  the
  99. "cls" etc  either since  the echo  │  CHECK2.BAT program for an example
  100. function has  been turned  off by  │  of this.
  101. the "echo off" command. All  they  │
  102. see  is   what   is  specifically  │     Like     other     programming
  103. echoed    to    the  screen.. the  │  languages,    batch  files  allow
  104. message  "Insert  next  disk" and  │  control   structures    so   that
  105. also  the  message    from    the  │  different  instructions   may  be
  106. PAUSE    and    CHKDSK  commands.  │  executed depending on the outcome
  107. Once      the      batch     file  │  of events. In the  above  example
  108. terminates,  DOS   goes  back  to  │  the    most    primitive  command
  109. echoing commands  by  itself; you  │  "GOTO"    was  used,  and  caused
  110. do not need to have a line at the  │  program execution to  pass to the
  111.  
  112.  
  113. specified   label,    which   was  │  echo This program checks multiple
  114. specified by  typing  the label's  │  disks in the %1 drive
  115. name   on   a  line   by   itself  │  :here
  116. preceded  by a colon.              │  pause Insert next disk and
  117.                                    │  chkdsk %1: /f
  118.    To  determine  whether  or not  │  if errorlevel 0 goto here
  119. to change  program execution, the  │  echo Bye!
  120. IF statement  is used.   Here's a  │
  121. better version  of  the CHECK.BAT  │     The   REM   statement   allows
  122. progam, using the IF statement.    │  remarks; lines  starting with REM
  123.                                    │  are not  executed but  just serve
  124. CHECK2.BAT:                        │  to  remind  the   user  what  the
  125.                                    │  program  is  for,   when  it  was
  126. rem CHECK2.BAT program written in  │  written, or  any  other pertinent
  127. July 1993                          │  information.
  128. rem Usage:  check2 <Drive>         │
  129. rem  Checks  disks  in  the drive  │     As long as  the error level is
  130. specified                          │  zero, the  program loops  back to
  131. @echo off                          │  "here".  Otherwise  it  continues
  132. cls                                │  running  and  thus  comes  to the
  133.  
  134.  
  135. next command, the Bye message and  │     The IF  statement can  also be
  136. finishes.   DOS returns  an error  │  used with  the NOT  parameter, eg
  137. level  of  0  from  the  previous  │  if not  errorlevel  3  goto start
  138. command    (CHKDSK    here)    if  │  Which would send the program back
  139. everything is okay and there were  │  to the  start  for any  exit code
  140. no  errors.  An  examples  of  an  │  except for a value of 3.
  141. error is the  case of there being  │
  142. no disk in the drive!              │     Yet  another  use  of  the  IF
  143.                                    │  statement is  to check  if a file
  144.    The  %1  indicates  the  first  │  exists or not:
  145. parameter;  ie   the  first  word  │  if not exist  %1.bak echo Warning
  146. typed after the  word "check2" on  │  no backup!
  147. the command  line.   In this case  │
  148. it is the drive identifier, eg A:  │  Another useful  control structure
  149. or B:  Up  to nine parameters can  │  is the FOR statement.
  150. be specified by  using %1 through  │  AUTOEXEC.BAT:
  151. to   %9.      The   %0  parameter  │  (this batch file is automatically
  152. automatically  contains  the name  │  run  whenever   the  computer  is
  153. of the batch file.                 │  powered on or booted)
  154.                                    │
  155.  
  156.  
  157. @echo off                          │     In    the    FOR    statement,
  158. break off                          │  parameters are preceded by %% and
  159. cls                                │  can be  any character  except for
  160. echo Checking  all  EXE  files in  │  characters 0 to 9.   In this case
  161. the root directory for viruses     │  the parameter was  called %%x and
  162. for  %%x  in   (\*.exe)  do  call  │  was used  so  that for  each .EXE
  163. vcheck %%x                         │  file in  the root  directory, the
  164.                                    │  virus checking batch file was run
  165.                                    │  by using the CALL command.
  166. VCHECK.BAT:                        │
  167. @echo off                          │     The reason that the errorlevel
  168. scan /nomem /nopause %1 > nul      │  statements are  as  they  are, is
  169. if  errorlevel   2  echo  Program  │  that the  if errorlevel statement
  170. Error (Out of memory?)             │  also  accepts  greater  values as
  171. if errorlevel 2 goto end           │  well as equal  values.   So if no
  172. if  errorlevel   1   pause  VIRUS  │  provision was made  for exit code
  173. FOUND! Reboot  from  clean system  │  2 (which occurs  when the program
  174. disk now                           │  is out of memory or similar), the
  175. :end                               │  "if errorlevel 1" statement would
  176.                                    │  be executed  and  the  user would
  177.  
  178.  
  179. think they had a virus.            │     Note that  the  examples given
  180.                                    │  are  not   necessarily  the  best
  181.    The   VCHECK.BAT   file   then  │  solutions,   they   were  written
  182. scanned  the  file   given  as  a  │  solely   to   illustrate  various
  183. parameter to  it  and if  a virus  │  facets of batch programming.
  184. was  found  (indicated  by  a non  │
  185. zero errorlevel)  then  a message  │  This concludes  the  tutorial,  I
  186. was displayed asking  the user to  │  hope that it has been useful ñ
  187. reboot  their   computer  from  a  │
  188. "clean" disk.   This is important  │
  189. in  case  the   virus  is  memory  │
  190. resident, in  which case accurate  │
  191. scanning may not  be possible and  │
  192. more files  may  become infected.  │
  193. Not all  virus  checking programs  │
  194. will  return  error  levels  like  │
  195. this,   these   exit   codes  are  │
  196. specific  to  McAfee's  excellent  │
  197. SCAN package.                      │
  198.                                    │
  199.